package com.qmyx.supply.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.model.request.MengWangRequestVo;
import com.ytjj.qmyx.supplychain.common.model.request.SanTongRequestVo;
import com.ytjj.qmyx.supplychain.common.model.request.XinycSmsRequest;
import com.ytjj.qmyx.supplychain.service.MqProducerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiawei
 * @date 2023年03月10日 17:17
 * @describe 回调接口
 */
@Slf4j
@RestController
@RequestMapping("/callback")
public class CallbackController {

    @Autowired
    MqProducerService msgFailLogService;


    /**
     * 欣易辰回调接口
     */
    @PostMapping("/xinyichenSms")
    public CommonResult xinyichenSms(@RequestBody List<XinycSmsRequest> request){
        log.info("===xinyichen回调Response==>{}",JSON.toJSONString(request));
        msgFailLogService.sendXinycMsgInfo(request);
        return CommonResult.success();
    }

    /**
     * 三通回调接口
     */
    @PostMapping("/santongSms")
    public Object santongSms(HttpServletRequest request){
        Map<String,Object> resp = new HashMap<>();
        try {
            String report = request.getParameter("report");
            log.info("===santong回调Response==>{}",report);
            JSONObject jsonObject = JSON.parseObject(report);
            Object obj = jsonObject.get("reports");
            List<SanTongRequestVo> sanTongRequestVos = JSONArray.parseArray(JSON.toJSONString(obj), SanTongRequestVo.class);
            msgFailLogService.sendSantongSms(sanTongRequestVos);
            resp.put("status","success");
        } catch (Exception e) {
            log.error("三通回调更新短信回调失败！====> {}",e);
            resp.put("status","fail");
        }
        return resp;
    }


    /**
     * 梦网回调接口
     */
    @PostMapping("/mengwangSms")
    public Object mengwangSms(String rpts,Integer seqid){
        log.info("请求：rpts"+rpts+"-->seqid"+seqid);
        Map<String,Object> resp = new HashMap<>();
        resp.put("cmd","RPT_RESP");
        resp.put("seqid",seqid);
        try {
            List<MengWangRequestVo> sanTongRequestVos = JSONArray.parseArray(rpts, MengWangRequestVo.class);
            msgFailLogService.sendMengWangSms(sanTongRequestVos);
            resp.put("result",0);
        } catch (Exception e) {
            log.error("梦网回调接更新短信回调失败！====> {}",e);
            resp.put("result",-1);
        }
        return resp;
    }
}
